<?php

namespace Form\Task;

use App\Dao\Exam\ExamDao;
use App\Dao\Task\TaskHelpDao;
use App\Dao\Task\TaskTypesDao;
use Form\BaseForm;
use App\Model\Task\TaskHelpCategory;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\ColumnBuilder\Table;
use Yj\ColumnBuilder\Templates\Field;
use Yj\ColumnBuilder\Templates\FieldTemplate;
use Yj\ColumnBuilder\Templates\InputTemplate;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/task")
 */
class Task extends \Yj\Form\BaseForm
{
    protected $yjCode = 'task/task';

    public function getColumn()
    {
        $colum = Column::create();

        $colum->setRules([
            Column::txt()->setLabel('任务名称')->setProp('title'),
            Column::txt()->setLabel('产品名称')->setProp('product_name'),
            Column::txt()->setLabel('商户名称')->setProp('user.nickname'),
            Column::txt()->setLabel('商户手机号码')->setProp('user.phone'),
            Column::txt()->setLabel('总金额')->setProp('price'),
            Column::txt()->setLabel('单价')->setProp('one_price'),
            Column::txt()->setLabel('库存')->setProp('stock'),
            Column::txt()->setLabel('开始时间')->setProp('create_date'),
            Column::txt()->setLabel('结束时间')->setProp('end_time_format'),
            Column::switch()->setLabel('是否显示')->setProp('is_open')->setUrl($this->createRoute('set-field-status')),
//            Column::switch()->setLabel('支付状态')->setProp('pay_status')->setUrl($this->createRoute('set-field-status')),
            Column::txt()->setLabel('状态')->setProp('status_format'),
        ]);

        return $colum->toArray();
    }

    public function getTabs()
    {
        return Table::header()->setProp('status', 'all')->setOptions([
            ['label' => '待付款', 'value' => 0],
            ['label' => '待审核', 'value' => 1],
            ['label' => '进行中', 'value' => 2],
            ['label' => '已完结', 'value' => 10],
            ['label' => '已驳回', 'value' => -1],
            ['label' => '已关闭', 'value' => -2],
        ])->toArray();
    }

    public function getSearch()
    {
        return Table::search()->setFields([
            Field::input()->setLabel('任务名称')->setVModel('keyword'),
            Field::input()->setLabel('商户名称')->setVModel('nickname'),
            Field::input()->setLabel('商户手机号')->setVModel('phone'),
            Field::select()->setLabel('支付状态')->setVModel('pay_status')->setOptions([
                ['label' => '已付款', 'value' => 1],
                ['label' => '未付款', 'value' => 0]
            ])
        ])->toArray();
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $routes = $this->getRoute();
        $column = $this->getColumn();
        $search = $this->getSearch();
        $tabs = $this->getTabs();
        _SUCCESS(compact('routes', 'column', 'tabs', 'search'));
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getParams()->get('id');
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));
        $form->setRule([
            Elm::YjUpload()->field('image_id')->title('图标'),
            Elm::input('title', '标题')->required(),
            Elm::textarea('desc', '内容')->required(),
        ]);
        $id && $form->append(
            Elm::hidden('id', $id)
        );
        $lists = $form->setTitle($id ? '编辑' : '添加' . '任务帮助')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(TaskTypesDao::class)->where('id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }
}